import { CoolController, BaseController } from '@cool-midway/core';
import { UserEntity } from '../../entity/user';
import { SchoolEntity } from '../../entity/school';
import { Get, Inject, Query } from '@midwayjs/core';
import { UserService } from '../../service/user';

/**
 * 用户管理
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: UserEntity,
  // 分页查询配置
  pageQueryOp: {
    // 模糊查询字段
    keyWordLikeFields: ['name', 'mobile', 'class', 'username', 'openid', 'nickname', 'school'],
    select: ['a.*', 'b.name as schoolName'],
    // 关联查询
    join: [
      {
        entity: SchoolEntity,
        alias: 'b',
        condition: 'a.schoolId = b.id',
        type: 'leftJoin',
      },
    ],
    // 添加排序
    addOrderBy: {
      id: 'asc'
    }
  }
})

export class UserController extends BaseController {
  @Inject()
  userService: UserService;

  /**
    * 用户信息
    * @param id 用户id
  */
  @Get('/findOrCreateUser')
  async findOrCreateUser(@Query('openid') openid: string) {
    return {
      name: await this.userService.findOrCreateUser(openid)
    }
  }
} 